import React, { useMemo } from "react";
import EditPage, { createFields } from "../../Component/ResourceList/Edit";
import { LibAppView } from '@/lib';
import { FormattedMessage } from "react-intl";
import lodash from 'lodash';
import { publishOwnerList } from "./StaticData";

const LawDocEdit = LibAppView.withService({
    reqEndPoints: [{
        endPoint: "setting.configList",
        name: "category",
        uri: { config: "lawDocType" }
    }]
})((props) => {
    const requireRules = useMemo(() => ([{ required: true, message: <FormattedMessage id="admin.pages.article.formItem.required"/>}]), []);
    const categoryList = useMemo(() => {
        const specialTypeData =  lodash.get(props.responseData, "category.data", []);
        return specialTypeData.map((item: any) => ({
            label: item.dataValue,
            value: item.name
        }));
    }, [props.responseData]);
    const fields = useMemo(() =>createFields([{
        label: "标题",
        name: "title",
        type: "Input",
        required: true
    },
    {
        label: "简介",
        name: "description",
        type: "TextArea"
    },{
        label: "发布主体",
        name: "publishOwner",
        type: "RadioGroup",
        required: true,
        props: {
            options: publishOwnerList
        }
    },{
        label: "类别",
        name: "category",
        type: "Select",
        required: true,
        props: {
            options: categoryList || []
        }
    }, {
        label: "内容",
        name: "content",
        type: "RichText",
        required: true
    }
]),[categoryList]);
    return (
        <EditPage
            submitEndPoint="resource.lawDocSubmit"
            fields={fields}
            initValues={{
                dataType: "specialType"
            }}
            buildRequest={(formData: any) => {
                return {
                    ...formData,
                    status: 1,
                    categoryTitle: categoryList?.find((v) => v.value === formData.category)?.label,
                    approveMsg: ""
                };
            }}
        />
    )
});

export default LawDocEdit;

